package com.kejiguan.ui.presenter;

import android.text.TextUtils;
import android.widget.Toast;

import com.android.volley.error.VolleyError;
import com.android.volley.response.Response;
import com.kejiguan.ui.model.UserManagerModer;
import com.kejiguan.ui.request.GsonRequest;
import com.kejiguan.ui.request.RequestUtil;
import com.kejiguan.ui.response.AddUserResponse;
import com.kejiguan.ui.response.DelUserResponse;
import com.kejiguan.ui.response.ModifyUserResponse;
import com.kejiguan.ui.response.QueryMenuResponse;
import com.kejiguan.ui.response.UserManagerListResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.util.DateFormatUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY on 2016/4/30.
 */
public class UserManagerFragmentPresenter extends BaseFragmentPresenter{


    public void queryMenus()
    {
        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Map<String,String> params=new HashMap<>();
        params.put("requestCode","707");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","queryList");


        final GsonRequest<QueryMenuResponse> request = new GsonRequest("查询菜单", QueryMenuResponse.class, new Response.Listener<QueryMenuResponse>() {
            @Override
            public void onResponse(QueryMenuResponse response) {
                response.initMenus();
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "查询菜单失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);

    }


    public void delUser(final UserManagerModer userManagerModer)
    {
        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","711");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","delete");
        params.put("uId",String.valueOf(userManagerModer.getUserId()));
        params.put("userId",userManagerModer.getUserName());



        final GsonRequest<DelUserResponse> request = new GsonRequest("queryUserList", DelUserResponse.class, new Response.Listener<DelUserResponse>() {
            @Override
            public void onResponse(DelUserResponse response) {
                response.setUserManagerModer(userManagerModer);
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "删除用户失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }


    public void modifyUser(final UserManagerModer userManagerModer)
    {
        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","711");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","mod");
        params.put("uId",String.valueOf(userManagerModer.getUserId()));
        params.put("userId",userManagerModer.getUserName());
        if (!TextUtils.isEmpty(userManagerModer.getPassword()))
        {
            params.put("userPassword",userManagerModer.getPassword());
        }

        params.put("userEmail",userManagerModer.getEmail());
        params.put("userTelephone",userManagerModer.getTelephone());
        params.put("navList",userManagerModer.getNavList());

        final GsonRequest<ModifyUserResponse> request = new GsonRequest("modUser", ModifyUserResponse.class, new Response.Listener<ModifyUserResponse>() {
            @Override
            public void onResponse(ModifyUserResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "修改用户失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

    public void addUser(final UserManagerModer userManagerModer)
    {
        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","711");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","add");
        params.put("userId",userManagerModer.getUserName());
        params.put("userPassword",userManagerModer.getPassword());
        params.put("userEmail",userManagerModer.getEmail());
        params.put("userTelephone",userManagerModer.getTelephone());
        params.put("navList",userManagerModer.getNavList());


        final GsonRequest<AddUserResponse> request = new GsonRequest("addUser", AddUserResponse.class, new Response.Listener<AddUserResponse>() {
            @Override
            public void onResponse(AddUserResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "新增用户失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }



    public void querySysUsers()
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","711");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","queryList");



        final GsonRequest<UserManagerListResponse> request = new GsonRequest("queryUserList", UserManagerListResponse.class, new Response.Listener<UserManagerListResponse>() {
            @Override
            public void onResponse(UserManagerListResponse response) {
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext, "查询用户失败！", Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }
}
